package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.ArticleDAO;
import dao.CommentDAO;
import dao.ContentDAO;
import dao.UserDAO;
import entity.Article;
import entity.Comment;
import entity.Content;
import entity.User;
import help.PageBean;
import help.ShowArticle;

/**
 * Servlet implementation class ShowTieServlet
 */
@WebServlet("/ShowTieServlet")
public class ShowTieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private ArticleDAO articleDAO = ArticleDAO.getInstance();
    private ContentDAO contentDAO = ContentDAO.getInstance();
    private CommentDAO commentDAO = CommentDAO.getInstance();
    private UserDAO userDAO = UserDAO.getInstance();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowTieServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		String page = (String)request.getParameter("pageNum");
		int pageNum =0;
		if(page==null){
			pageNum=1;
		}
		else{
			pageNum=Integer.parseInt(page);
		}
		//HttpSession session = (HttpSession)request.getSession(true);
		int article_id=Integer.parseInt((String)request.getParameter("article_id"));
		//int article_id=10;
		articleDAO.modifyClickTime(article_id);
		Article article = articleDAO.get(article_id);//得到这个文章的基本信息
		//给这个帖子的浏览量加1 
		Content content = contentDAO.get(article_id);//得到文章的内容
		System.out.println(article.toString());
		System.out.println(content.toString());
		//需要考虑一个问题，就是帖子的分页，实现逻辑，。如果把pageBean给一个标签，就很麻烦，写一个有body体的标签，再写一个footer。
		
		//先构建一个pagebean
		
		List<Comment> comments = commentDAO.get(article_id);
		System.out.println(comments.toString());
		PageBean<Comment> pageBean = new PageBean<>(pageNum, 15, comments.size(), 2);
		int Pendindex = pageBean.getStartIndex()+pageBean.getPagesize();
		int endIndex =(Pendindex<=comments.size())?Pendindex:comments.size();
		System.out.println("前："+pageBean.getStartIndex()+"     "+"后："+endIndex);
		pageBean.setList(comments.subList(pageBean.getStartIndex(),endIndex));
		//pageBean.setList(comments.subList(1,2));
		System.out.println(pageBean.getList().toString());
		ShowArticle showArticle = new ShowArticle(article,content);
		request.setAttribute("author",userDAO.get(article.getUser_id()));
		request.setAttribute("showArticle", showArticle);
		request.setAttribute("pageBean", pageBean);
		
		request.getRequestDispatcher("ShowTie.jsp?article_id="+article_id).forward(request, response);
		//response.sendRedirect("ShowTie.jsp");
				
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
